package com.main.locatme;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.Toast;

import com.locatme.model.Enseigne;
import com.locatme.model.Personne;
import com.locatme.model.Utilisateur;
import com.locatme.modelBD.UtilisateurBD;

public class LoginActivity extends Activity {

    private UtilisateurBD userBD;
    private Utilisateur user;
    private String login;
    private String password;
    private int checkedId;
    private int type = 1;


    public UtilisateurBD getUserBD() {
        return userBD;
    }

    public void setUserBD(UtilisateurBD userBD) {
        this.userBD = userBD;
    }

    public Utilisateur getUser() {
        return user;
    }

    public void setUser(Utilisateur user) {
        this.user = user;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Bundle extras = getIntent().getExtras();
        if (extras != null) {
            if(extras.getString("compte_cree").equals("oui")){
                Toast.makeText(LoginActivity.this, "Account created, you can log in", Toast.LENGTH_LONG).show();
            }
        }
        setContentView(R.layout.activity_login);
        final Button createButton = (Button) findViewById(R.id.create_account);
        final Button createProButton= (Button) findViewById(R.id.create_account_p);
        final Button loginButton = (Button) findViewById(R.id.connect);
        final EditText loginEditText = (EditText) findViewById(R.id.user_email);
        final EditText passEditText = (EditText) findViewById(R.id.user_password);
        final RadioButton rd1 = (RadioButton) findViewById(R.id.personne);
        final RadioButton rd2 = (RadioButton) findViewById(R.id.enseigne);

        rd1.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                rd2.setChecked(false);
                type = 1;
            }
        });

        rd2.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                rd1.setChecked(false);
                type = 2;

            }
        });

        //Creation de compte
        createButton.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                Intent intent;
                intent = new Intent(LoginActivity.this, CreateActivity.class);
                startActivity(intent);
            }

        });

        createProButton.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                Intent intent;
                intent = new Intent(LoginActivity.this, CreateEnseigneActivity.class);
                startActivity(intent);
            }

        });

        //Authentification
        loginButton.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                if (loginEditText.getText().toString().isEmpty() || passEditText.getText().toString().isEmpty()) {
                    Toast.makeText(LoginActivity.this, "Please fill all required fields", Toast.LENGTH_LONG).show();
                } else {
                    login = loginEditText.getText().toString();
                    password = passEditText.getText().toString();
                    if(valider(v)){
                        Intent intent = new Intent(LoginActivity.this, MainActivity.class);
                        if(type==1){
                            intent.putExtra("user",(Personne) user);
                        }
                        else{
                            intent.putExtra("user",(Enseigne) user);
                        }
                        startActivity(intent);
                    } else {
                        Toast.makeText(LoginActivity.this, "User not found, try another login/password", Toast.LENGTH_LONG).show();
                    }
                }
            }

        });


    }//Fin on create

    public String getType()
    {
        String t = null;
        if(type==1)
        {
            t = "Personne";
        }
        else if(type==2)
        {
            t = "Enseigne";
        }

        return t;
    }

    public boolean valider(View v) {
        boolean etat;
        userBD = new UtilisateurBD(this);

        switch (type) {
            case 1:
                user = new Personne();
                userBD.open();
                user = userBD.verifPersonne(login,password);
                break;

            case 2:
                user = new Enseigne();
                userBD.open();
                user = userBD.VerifEnseigne(login,password);
                break;
        }
        if (user!=null) {
            etat = true;
        } else {
            etat = false;
        }
        userBD.close();
        return etat;

    }

}
